VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Speech"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
    Option Explicit







'Speech engine
Public m_SpeechEngine As Byte
'0: no speech, 1: jaws, 2: WinEyes,
' 3: SA, 4: NVDA, 5: sapi

'Sapi variables
Public m_voice As SpeechLib.SpVoice
Public m_Token As SpeechLib.ISpeechObjectToken
Public m_Position As Variant
Public m_NumberOfVoices As Variant
Public SVoice As Integer
Public m_Menu As String

'Declaration for JFWApi
Private Declare Function JSpeak Lib "jfwapi32.dll" Alias "JFWSayString" (ByVal StringToSpeak As String, Optional ByVal blnInterrupt As Boolean) As Long
Private Declare Function JStop Lib "jfwapi32.dll" Alias "JFWStopSpeech" () As Long
Private Declare Function JRunScript Lib "jfwapi32.dll" Alias "JFWRunScript" (ByVal ScriptName As String) As Long
'WE
Dim WeObject
     
     'SA declarations
Private Declare Function SA_Say Lib "SAAPI32.dll" Alias "SA_SayA" (SayWhat As String) As Long
Private Declare Function SA_IsRunning Lib "SAAPI32.dll" () As Boolean
Private Declare Function SA_StopAudio Lib "SAAPI32.dll" () As Long
'NVDA declarations
Private Declare Function nvdaController_testIfRunning Lib "nvdaControllerClient32.dll" () As Long
Private Declare Function nvdaController_speakText Lib "nvdaControllerClient32.dll" (ByVal StrToSpeak As Long) As Long
Private Declare Function nvdaController_cancelSpeech Lib "nvdaControllerClient32.dll" () As Long

Public Sub Initialize()
Select Case m_SpeechEngine
Case Is = 1
JFWCheck
Case Is = 2
wecheck
Case Is = 3
SA_Check
Case Is = 4
InitNVDA
Case Is = 5
LoadSapi
End Select
 End Sub


Public Sub JFWCheck()
'A sub to check that JFW is loaded or not.
Dim lngLON As Long
lngLON = JStop
If lngLON = 0 Then
MsgBox SPrintF(lang.Messages(85), "Jaws for Windows"), vbCritical + vbOKOnly, lang.Messages(86)
m_SpeechEngine = 0
End If
End Sub



'Code for Microsoft Speech Api

Public Sub LoadSapi()
'A sub to set up sapi


Set m_voice = New SpVoice
If m_voice Is Nothing Then
MsgBox SPrintF(lang.Messages(85), "the Microsoft Text To Speech Api"), vbCritical + vbOKOnly, lang.Messages(86)
End If

For Each m_Token In m_voice.GetVoices
m_Position = m_Position + 1
Next m_Token
m_NumberOfVoices = m_Position - 1
LoadSapiSettings
End Sub

Public Sub Say(strStringToSpeak As String, Optional interrupt As Boolean = False)

Select Case m_SpeechEngine
Case Is = 1
If interrupt = True Then
JSpeak strStringToSpeak, True
Else
JSpeak strStringToSpeak
End If
Case Is = 2
If interrupt = True Then
wStopSpeech
wSay strStringToSpeak
ElseIf interrupt = False Then
wSay strStringToSpeak
End If
Case Is = 3
If interrupt = True Then
SA_StopAudio
SA_Say strStringToSpeak
Else
SA_Say strStringToSpeak
End If

Case Is = 4
If interrupt = True Then
nvdaController_cancelSpeech
nvdaController_speakText (StrPtr(strStringToSpeak))
Else
nvdaController_speakText (StrPtr(strStringToSpeak))
End If

Case Is = 5
If interrupt = True Then
StopSpeech
On Error GoTo sapierr
m_voice.Speak strStringToSpeak, SVSFlagsAsync
ElseIf interrupt = False Then
        On Error GoTo sapierr
        m_voice.Speak strStringToSpeak
End If
End Select

Exit Sub

sapierr:
MsgBox lang.Messages(84), vbOKOnly + vbCritical, lang.Messages(86)

End Sub

Public Sub StopSpeech()
Select Case m_SpeechEngine
Case Is = 1
JStop
Case Is = 2
wStopSpeech
Case Is = 3
SA_StopAudio
Case Is = 4
nvdaController_cancelSpeech
Case Is = 5
On Error GoTo sapierr
m_voice.Speak vbNullString, SVSFPurgeBeforeSpeak
End Select
Exit Sub

sapierr:
MsgBox lang.Messages(84), vbOKOnly + vbCritical, lang.Messages(86)

End Sub

Public Sub SMUp()
If m_SpeechEngine <> 3 Then Exit Sub

If m_Menu = "main" Then
If m_Position <= 1 Then
m_Position = 1
Exit Sub
Else
m_Position = m_Position - 1
End If
Select Case m_Position
Case Is = 1
Say "Adjust speed", True
Case Is = 2
Say "Adjust voice", True
Case Is = 3
Say "Adjust volume", True
Case Is = 4
Say "Restore defaults", True
Case Is = 5
Say "exit", True
End Select
End If


If m_Menu = "speed" Then
If m_voice.Rate = 100 Then
m_voice.Rate = 100
Exit Sub
Else
m_voice.Rate = m_voice.Rate + 1
End If
Say m_voice.Rate, True
End If

If m_Menu = "volume" Then
If m_voice.volume = 100 Then
m_voice.volume = 100
Exit Sub
Else
m_voice.volume = m_voice.volume + 1
End If
Say m_voice.volume, True
End If

If m_Menu = "voice" Then
m_Position = m_Position - 1
If m_Position < 0 Then
m_Position = 0
Exit Sub
End If
Set m_voice.Voice = m_voice.GetVoices().Item(m_Position)
Say m_voice.Voice.GetDescription, True
End If
End Sub

Public Sub SMDown()
If m_SpeechEngine <> 3 Then Exit Sub

If m_Menu = "main" Then
If m_Position >= 5 Then
m_Position = 5
Exit Sub
Else
m_Position = m_Position + 1
End If
Select Case m_Position
Case Is = 1
Say "Adjust speed", True
Case Is = 2
Say "Adjust voice", True
Case Is = 3
Say "Adjust volume", True
Case Is = 4
Say "Restore defaults", True
Case Is = 5
Say "exit", True
End Select
End If

If m_Menu = "volume" Then
If m_voice.volume = 0 Then
m_voice.volume = 0
Exit Sub
Else
m_voice.volume = m_voice.volume - 1
End If
Say m_voice.volume, True
End If

If m_Menu = "speed" Then
If m_voice.Rate = -100 Then
m_voice.Rate = -100
Exit Sub
Else
m_voice.Rate = m_voice.Rate - 1
End If
Say m_voice.Rate, True
End If

If m_Menu = "voice" Then
m_Position = m_Position + 1
If m_Position > m_NumberOfVoices Then
m_Position = m_NumberOfVoices
Exit Sub
End If
Set m_voice.Voice = m_voice.GetVoices().Item(m_Position)
Stop
Say m_voice.Voice.GetDescription, True
End If
End Sub

Public Sub SMEnter()
If m_SpeechEngine = 3 Then
If m_Menu = "speed" Then
Say "The speed has been changed."
m_Menu = "main"
Say "please choose an option:"
m_Position = 1
Say "Adjust speed", True
Exit Sub
End If
If m_Menu = "voice" Then
SVoice = m_Position
Say "The voice has been changed."
m_Menu = "main"
Say "please choose an option:"
m_Position = 1
Say "Adjust speed", True
Exit Sub
End If
If m_Menu = "volume" Then
Say "The volume has been changed."
m_Menu = "main"
Say "please choose an option:"
m_Position = 1
Say "Adjust speed", True
Exit Sub
End If
If m_Menu = "main" Then
Select Case m_Position
Case Is = 1
m_Menu = "speed"
Say "speed settings. " & m_voice.Rate, True
Exit Sub
Case Is = 2
m_Position = 0
m_Menu = "voice"
Say "voice settings."
Set m_voice.Voice = m_voice.GetVoices().Item(m_Position)
Say m_voice.Voice.GetDescription, True
Exit Sub
Case Is = 3
m_Menu = "volume"
Say "volume settings."
Say m_voice.volume, True
Exit Sub
Case Is = 4
restore_defaults
Exit Sub
Case Is = 5
Say "Settings saved", True
m_Menu = ""
SaveSapiSettings
Exit Sub
End Select
End If

Else
Say "This feature is only available with sapi", True
End If
End Sub

Public Sub restore_defaults()
m_Position = 0
m_voice.Rate = 0
Set m_voice.Voice = m_voice.GetVoices().Item(m_Position)
m_voice.volume = 100
Say "The default settings has been restored successfully"
Say "Please choose an option"
Say "Adjust speed", True
m_Position = 1
End Sub

Public Sub LoadSapiSettings()
Dim speed As Integer
Dim volume As Integer
If Dir(App.Path & "\spcfg.dat") = "SPCfg.dat" Then
Open App.Path & "\spcfg.dat" For Input As #1
Input #1, speed
Input #1, SVoice
Input #1, volume
m_voice.Rate = speed
Set m_voice.Voice = m_voice.GetVoices().Item(SVoice)
m_voice.volume = volume
Close #1
End If
End Sub

Public Sub SaveSapiSettings()
Open App.Path & "\SPCfg.dat" For Output As #1
Write #1, m_voice.Rate, SVoice, m_voice.volume
Close #1

End Sub




Public Sub SapiMenu()
If m_SpeechEngine = "sapi" Then
m_Position = 1
Say "use the up and down arrow keys to select an option, then press enter. Adjust speed.", True
m_Menu = "main"
End If
End Sub



Public Property Get Engine() As String
Engine = m_SpeechEngine
End Property

Public Property Let Engine(SPE As String)
m_SpeechEngine = SPE
End Property

Public Sub JFWRunScript(ScriptName As String)
JRunScript ScriptName
End Sub


Sub wSay(StrStringToSay As String)
     WeObject.speakstring (StrStringToSay)
End Sub

Sub wStopSpeech()
WeObject.silence
End Sub

Public Sub wecheck()
If Not IsProcessRunning("wineyes.exe") Then
                            MsgBox SPrintF(lang.Messages(85), "Window Eyes"), vbCritical + vbOKOnly, lang.Messages(86)
                                        m_SpeechEngine = 0
                                        Else
                                        Set WeObject = CreateObject("GWSpeak.Speak")
                                        End If
                                        End Sub


Private Sub SA_Check()
If SA_IsRunning = False Then
MsgBox SPrintF(lang.Messages(85), "System Access"), vbCritical + vbOKOnly, lang.Messages(86)
m_SpeechEngine = 0
End If
End Sub

Private Sub InitNVDA()
If nvdaController_testIfRunning <> 0 Then
MsgBox SPrintF(lang.Messages(85), "NVDA"), vbCritical + vbOKOnly, lang.Messages(86)
m_SpeechEngine = 0
End If

End Sub

Public Sub Unload()
Select Case m_SpeechEngine
Case Is = 2
Set WeObject = Nothing
Case Is = 5
Set m_voice = Nothing
Set m_Token = Nothing
End Select

End Sub
